from os import write
from rest_framework import serializers
from api.models import CloudSecurityGroups
from rest_framework.exceptions import ValidationError
from api.extension import return_code
from rest_framework.response import Response


class CloudSecurityUpdateSerializer(serializers.ModelSerializer):
    description = serializers.CharField(label="描述信息", write_only=True)
    remote_ip_prefix = serializers.IPAddressField(
        label="IP地址", write_only=True)
    cloud_env = serializers.CharField(label="环境", write_only=True)

    class Meta:
        model = CloudSecurityGroups
        fields = ['description', 'remote_ip_prefix', 'cloud_env']

    def validate_description(self, value):
        exsit = CloudSecurityGroups.objects.filter(description=value).exists()
        if not exsit:
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "detail": "description 不存在，输入自己的名字"})
        return value

    def validate_cloud_env(self, value):
        if not value:
            return ValidationError({"code": return_code.VALIDATE_ERROR, "error": "需要输入环境变量"})
        if not value == 'dev' and not value == 'prod':
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "error": "环境变量只支持dev/prod"})
        if value == 'dev':
            value = "2"
        elif value == 'prod':
            value = "1"
        return value

    def validate_remote_ip_prefix(self, value):
        cloud_env = self.initial_data.get("cloud_env")
        exsit = CloudSecurityGroups.objects.filter(
            remote_ip_prefix=value, cloud_env=cloud_env).exists()
        if not exsit:
            return value
        raise ValidationError(
            {"code": return_code.VALIDATE_ERROR, "detail": "当前环境，IP地址已经存在无需添加安全组"})


class CloudSecurityCreateSerializer(serializers.ModelSerializer):
    description = serializers.CharField(label="描述信息", write_only=True)
    remote_ip_prefix = serializers.IPAddressField(
        label="IP地址", write_only=True)
    cloud_env = serializers.CharField(label="环境", write_only=True)

    class Meta:
        model = CloudSecurityGroups
        fields = ['description', 'remote_ip_prefix', 'cloud_env']

    def validate_cloud_env(self, value):
        if not value:
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "error": "需要输入环境变量"})
        if not value == 'dev' and not value == 'prod':
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "error": "环境变量只支持dev/prod"})
        if value == 'dev':
            value = "2"
        elif value == 'prod':
            value = "1"
        return value

    def validate_remote_ip_prefix(self, value):
        cloud_env = self.initial_data.get("cloud_env")
        exsit = CloudSecurityGroups.objects.filter(
            remote_ip_prefix=value, cloud_env=cloud_env).exists()
        if not exsit:
            return value
        raise ValidationError(
            {"code": return_code.VALIDATE_ERROR, "detail": "当前环境，IP地址已经存在无法创建新策略"})


class CloudCdnSerializer(serializers.Serializer):
    # 数据校验 urlsPath
    urlspath = serializers.URLField(label="域名", write_only=True)
    cloud_env = serializers.CharField(label="系统环境", write_only=True)

    def validate_urlspath(self, value):

        return value

    def validate_cloud_env(self, value):
        if not value:
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "error": "需要输入环境变量"})
        if not value == 'dev' and not value == 'prod':
            raise ValidationError(
                {"code": return_code.VALIDATE_ERROR, "error": "环境变量只支持dev/prod"})
        if value == 'dev':
            value = "2"
        elif value == 'prod':
            value = "1"
        return value
